# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


root=TreeNode(5,None,None)
root.left=TreeNode(1,None,None)
root.right=TreeNode(4,None,None)
root.right.left=TreeNode(3,None,None)
root.right.right=TreeNode(6,None,None)


def dfs(root,ans):
    if root.left==None and root.right==None:
        print("root.val:",root.val)
        return [root.val,root.val]
    if root.left!=None and root.right!=None:
        leftVal=dfs(root.left,ans)
        rightVal=dfs(root.right,ans)
        print(leftVal,root.val,rightVal)
        if root.val>leftVal and root.val<rightVal:
            #正确的情况
            return [leftVal,rightVal]
        else:
            ans[0]=False
    if root.right!=None and root.right==None:
        rightVal=dfs(root.right,ans)
        if root.val<rightVal:
            return [root.val,rightVal]
        else:
            ans[0]=False
    if root.left!=None and root.right==None:
        leftVal=dfs(root.left,ans)
        if root.val>leftVal:
            return [leftVal,root.val]
        else:
            ans[0]=False


def isValidBST(root):
    ans=[]
    ans.append(True)
    dfs(root,ans)
    print(ans[0])

isValidBST(root)